home *** CD-ROM | disk | FTP | other *** search
- /* file hqxtool.c
- BinHex decoder/encoder routines, example MPW Tool.
- Copyright (c) 1995, 1996, 1997 by John Montbriand. All Rights Reserved.
- Permission hereby granted for public use.
- Distribute freely in areas where the laws of copyright apply.
- USE AT YOUR OWN RISK.
- DO NOT DISTRIBUTE MODIFIED COPIES.
- Comments/questions/postcards* to the author at the address:
- John Montbriand
- P.O. Box. 1133
- Saskatoon Saskatchewan Canada
- S7K 3N2
- or by email at:
- tinyjohn@sk.sympatico.ca
- *if you mail a postcard, then I will provide you with technical support
- regarding questions you may have about this file.
- */
-
- #include <Types.h>
- #include <QuickDraw.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Gestalt.h>
- #include <TextUtils.h>
- #include <Errors.h>
- #include <Memory.h>
- #include <strings.h>
- #include <ErrMgr.h>
- #include <StdIO.h>
- #include <PLStringFuncs.h>
- #include <string.h>
- #include <CursorCtl.h>
-
- #include "hqx.h"
-
- FILE *gDestFile, *gSrcFile;
- QDGlobals qd;
- FSSpec targetfile;
- Boolean replace_names, target_exists;
-
-
- static OSErr MySink(void* buffer, long count, long param) {
- SpinCursor(16);
- fwrite(buffer, count, 1, gDestFile);
- return noErr;
- }
-
- static OSErr MySource(void* buffer, long *count, long param) {
- SpinCursor(16);
- *count = fread(buffer, 1, *count, gSrcFile);
- return noErr;
- }
-
- static char** GetFullPath(StringPtr objectname, short vol, long dir) {
- Handle h;
- CInfoPBRec cat;
- Str255 separator;
- Str63 name;
- long response;
- Boolean haveAUX;
- h = NewHandle(0);
- if (objectname != NULL) PtrToXHand(objectname+1, h, objectname[0]);
- haveAUX = (Gestalt(gestaltAUXVersion, &response) == noErr);
- PLstrcpy(separator, haveAUX ? "\p/" : "\p:");
- cat.hFileInfo.ioNamePtr = name;
- cat.hFileInfo.ioVRefNum = vol;
- cat.hFileInfo.ioFlParID = dir;
- cat.hFileInfo.ioFDirIndex = -1;
- do { cat.hFileInfo.ioDirID = cat.hFileInfo.ioFlParID;
- if (PBGetCatInfoSync(&cat) != noErr) break;
- Munger(h, 0, NULL, 0, separator+1, separator[0]);
- Munger(h, 0, NULL, 0, name+1, name[0]);
- } while (cat.hFileInfo.ioDirID != 2);
- return (char**) h;
- }
-
- static OSErr MyNameFilter(StringPtr name, short *vol, long *dir, long param) {
- FInfo info;
- if (target_exists) {
- PLstrcpy(name, targetfile.name);
- *vol = targetfile.vRefNum;
- *dir = targetfile.parID;
- }
- if (replace_names) return noErr;
- if (HGetFInfo(*vol, *dir, name, &info) == noErr) {
- char** pathname;
- Str255 tname;
- long n;
- if ((pathname = GetFullPath(name, *vol, *dir)) != NULL) {
- n = GetHandleSize((Handle) pathname);
- if (n > 255) n = 255;
- tname[0] = n;
- BlockMoveData(*pathname, tname+1, n);
- ParamText(tname, NULL, NULL, NULL);
- DisposeHandle((Handle) pathname);
- } else ParamText(name, NULL, NULL, NULL);
- InitCursor();
- if (Alert(129, NULL) == ok) {
- return HDelete(*vol, *dir, name);
- } else return userCanceledErr;
- }
- return noErr;
- }
-
- #define kInputBufferSize (8*1024)
- #define kOutputBufferSize (8*1024)
-
- int main(long argc, char** argv) {
- long i;
- OSErr err;
- FSSpec src;
- char* dst;
- Str255 name;
- Boolean srcexists, decodecomplete;
- char *input_buffer, *output_buffer;
-
- InitGraf(&qd.thePort);
-
- dst = NULL;
- i = 1;
- gSrcFile = NULL;
- replace_names = target_exists = false;
- srcexists = false;
- decodecomplete = false;
- input_buffer = output_buffer = NULL;
-
- if (argc == 1) {
- fprintf(stderr, "# %s, binhex tool.\n", argv[0]);
- fprintf(stderr, "# %s variants:\n", argv[0]);
- fprintf(stderr, "# -y avoids replace alert in decoding.\n");
- fprintf(stderr, "# %s [-y] -d file -- decode the binhex file\n", argv[0]);
- fprintf(stderr, "# %s [-y] -d file as newname -- decode the binhex file to newname\n", argv[0]);
- fprintf(stderr, "# %s file > file -- encode file to stdout\n", argv[0]);
- fprintf(stderr, "# %s, file -o file -- encode file to filename\n", argv[0]);
- return 0;
- }
-
- while (i < argc) {
- if (strcmp(argv[i], "-y") == 0) {
- replace_names = true;
- i += 1;
- } else if (strcmp(argv[i], "-d") == 0) {
- char *headerstring = "(This file must be converted with BinHex 4.0)";
- if (i+1 >= argc) {
- fprintf(stderr, "#%s, parameters expected for -d\n", argv[0]);
- return 1;
- }
- if (output_buffer == NULL) {
- output_buffer = (char*) NewPtr(kOutputBufferSize);
- if ((err = MemError()) != noErr) goto misc_abort;
- }
- gSrcFile = fopen(argv[i+1], "r");
- if (gSrcFile == NULL) { err = fnfErr; goto misc_abort; }
- setvbuf(gSrcFile, output_buffer, _IOFBF, kOutputBufferSize);
- if ((i+2 < argc) && strcmp(argv[i+2], "as") == 0) {
- if (i+3 >= argc) {
- fprintf(stderr, "#%s, name expected for -d %s as...\n", argv[0], argv[i+1]);
- return 1;
- }
- err = FSMakeFSSpec(0, 0, c2pstr(strcpy((char*) name, argv[i+3])), &targetfile);
- if (err == fnfErr) err = noErr;
- if (err != noErr) goto misc_abort;
- target_exists = true;
- err = HQXDecode(MySource, MyNameFilter, replace_names, true, 0);
- if (err != noErr) goto misc_abort;
- i += 4;
- } else {
- err = HQXDecode(MySource, MyNameFilter, replace_names, true, 0);
- if (err != noErr) goto misc_abort;
- i += 2;
- }
- fclose(gSrcFile);
- DisposePtr((Ptr) output_buffer);
- output_buffer = NULL;
- gSrcFile = NULL;
- decodecomplete = true;
- } else if (strcmp(argv[i], "-o") == 0) {
- if (i+1 >= argc) {
- fprintf(stderr, "#%s, parameters expected for -o\n", argv[0]);
- return 1;
- }
- dst = argv[i+1];
- i += 2;
- } else if (!srcexists) {
- err = FSMakeFSSpec(0, 0, c2pstr(strcpy((char*)name, argv[i])), &src);
- if (err != noErr) goto misc_abort;
- srcexists = true;
- i += 1;
- } else {
- fprintf(stderr, "#%s, unknown parameter '%s'\n", argv[0], argv[i]);
- return 1;
- }
- }
-
- if (decodecomplete) return 0;
-
- input_buffer = (char*) NewPtr(kInputBufferSize);
- if ((err = MemError()) != noErr) goto misc_abort;
- if (srcexists) {
- if (dst == NULL) {
- gDestFile = stdout;
- setvbuf(gDestFile, input_buffer, _IOFBF, kInputBufferSize);
- err = HQXEncode(src.name, src.vRefNum, src.parID, MySink, 0);
- if (err != noErr) goto misc_abort;
- } else {
- gDestFile = fopen(dst, "w");
- setvbuf(gDestFile, input_buffer, _IOFBF, kInputBufferSize);
- err = HQXEncode(src.name, src.vRefNum, src.parID, MySink, 0);
- fclose(gDestFile);
- if (err != noErr) goto misc_abort;
- }
- } else {
- fprintf(stderr, "#%s, no source file specified\n", argv[0], argv[i]);
- return 1;
- }
-
- return 0;
-
- misc_abort:
- if (gSrcFile != NULL) fclose(gSrcFile);
- if (output_buffer != NULL) DisposePtr((Ptr) output_buffer);
- if (input_buffer != NULL) DisposePtr((Ptr) input_buffer);
- if (err == userCanceledErr) return 1;
- fprintf(stderr, "# %s aborted.\n# %s\n", argv[0], GetSysErrText(err, (char*) name));
- return 2;
- }
-
- /* end of file hqxtool.c */
-
-